package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.security.domain.AfElevator;
import com.ruoyi.security.domain.AfElevatorError;
import com.ruoyi.security.mapper.AfElevatorErrorMapper;
import com.ruoyi.security.mapper.AfElevatorMapper;
import com.ruoyi.security.service.IAfElevatorErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电梯故障记录Service业务层处理
 *
 * @author cjie
 * @date 2022-07-18
 */
@Service
public class AfElevatorErrorServiceImpl implements IAfElevatorErrorService {
    @Autowired
    private AfElevatorErrorMapper afElevatorErrorMapper;

    @Autowired
    private AfElevatorMapper afElevatorMapper;

    /**
     * 查询电梯故障记录
     *
     * @param id 电梯故障记录ID
     * @return 电梯故障记录
     */
    @Override
    public AfElevatorError selectAfElevatorErrorById(String id) {
        return afElevatorErrorMapper.selectAfElevatorErrorById(id);
    }

    /**
     * 查询电梯故障记录列表
     *
     * @param afElevatorError 电梯故障记录
     * @return 电梯故障记录
     */
    @Override
    public List<AfElevatorError> selectAfElevatorErrorList(AfElevatorError afElevatorError) {
        return afElevatorErrorMapper.selectAfElevatorErrorList(afElevatorError);
    }

    /**
     * 新增电梯故障记录
     *
     * @param afElevatorError 电梯故障记录
     * @return 结果
     */
    @Override
    public int insertAfElevatorError(AfElevatorError afElevatorError) {
        afElevatorError.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afElevatorError.setCreateTime(DateUtils.getNowDate());
        afElevatorError.setCreateBy(SecurityUtils.getUsername());
        return afElevatorErrorMapper.insertAfElevatorError(afElevatorError);
    }

    @Override
    public int insertErrorMsg(AfElevatorError afElevatorError) {
        afElevatorError.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afElevatorError.setCreateTime(DateUtils.getNowDate());
        //更新电梯状态，
        AfElevator afElevator = new AfElevator();
        afElevator.setId(afElevatorError.getElevatorId());
        afElevator.setState("故障");
        afElevatorMapper.updateAfElevator(afElevator);
        return afElevatorErrorMapper.insertAfElevatorError(afElevatorError);
    }

    /**
     * 修改电梯故障记录
     *
     * @param afElevatorError 电梯故障记录
     * @return 结果
     */
    @Override
    public int updateAfElevatorError(AfElevatorError afElevatorError) {
        afElevatorError.setUpdateTime(DateUtils.getNowDate());
        afElevatorError.setUpdateBy(SecurityUtils.getUsername());
        return afElevatorErrorMapper.updateAfElevatorError(afElevatorError);
    }

    /**
     * 批量删除电梯故障记录
     *
     * @param ids 需要删除的电梯故障记录ID
     * @return 结果
     */
    @Override
    public int deleteAfElevatorErrorByIds(String[] ids) {
        return afElevatorErrorMapper.deleteAfElevatorErrorByIds(ids);
    }

    /**
     * 删除电梯故障记录信息
     *
     * @param id 电梯故障记录ID
     * @return 结果
     */
    @Override
    public int deleteAfElevatorErrorById(String id) {
        return afElevatorErrorMapper.deleteAfElevatorErrorById(id);
    }

    @Override
    public Map<String, Integer> getTypeRatio() {
        Map<String, Integer> results = new HashMap<>();
        List<AfElevatorError> errors = afElevatorErrorMapper.getTypeRatio();
        for(AfElevatorError error :errors){
            results.put(error.getErrorCode(),error.getTotal());
        }

        Integer sum = 0;
        //求和，计算百分比
        for(Integer value:results.values()){
            sum += value;
        }
        for(Map.Entry<String,Integer> entry:results.entrySet()){
            entry.setValue(entry.getValue()*100/sum);
        }
        return results;
    }
}
